package br.inatel.ec203.projeto.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PartNumberSearchPanel extends JPanel {

	private static final long serialVersionUID = -7194870541725937900L;

	private JLabel partNumberLabel;
	private JTextField partNumberTextField;
	private JButton searchButton;
	
	private PartNumberSearchPanelListeners partNumberSearchPanelListeners;
	
	private GridBagConstraints partNumberLabelConstraints;
	private GridBagConstraints partNumberTextFieldConstraints;
	private GridBagConstraints searchButtonConstraints;
	
	public PartNumberSearchPanel() {
		initialize();
	}

	private void initialize() {
		setLayout(new GridBagLayout());
		add(getPartNumberLabel(), getPartNumberLabelConstraints());
		add(getPartNumberTextField(), getPartNumberTextFieldConstraints());
		add(getSearchButton(), getSearchButtonConstraints());
	}
	
	private JLabel getPartNumberLabel() {
		if (partNumberLabel == null) {
			partNumberLabel = new JLabel();
			partNumberLabel.setText("Part Number");
		}
		return partNumberLabel;
	}

	public JTextField getPartNumberTextField() {
		if (partNumberTextField == null) {
			partNumberTextField = new JTextField();
		}
		return partNumberTextField;
	}

	private JButton getSearchButton() {
		if (searchButton == null) {
			searchButton = new JButton();
			searchButton.setText("Pesquisar");
			searchButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					partNumberSearchPanelListeners.searchPerformed();
				}
			});
		}
		return searchButton;
	}

	private GridBagConstraints getPartNumberLabelConstraints() {
		if (partNumberLabelConstraints == null) {
			partNumberLabelConstraints = new GridBagConstraints();
			partNumberLabelConstraints.gridx = 0;
			partNumberLabelConstraints.gridy = 0;
			partNumberLabelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return partNumberLabelConstraints;
	}

	private GridBagConstraints getPartNumberTextFieldConstraints() {
		if (partNumberTextFieldConstraints == null) {
			partNumberTextFieldConstraints = new GridBagConstraints();
			partNumberTextFieldConstraints.gridx = 1;
			partNumberTextFieldConstraints.gridy = 0;
			partNumberTextFieldConstraints.ipadx = 250;
			partNumberTextFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
			partNumberTextFieldConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return partNumberTextFieldConstraints;
	}

	private GridBagConstraints getSearchButtonConstraints() {
		if (searchButtonConstraints == null) {
			searchButtonConstraints = new GridBagConstraints();
			searchButtonConstraints.gridx = 2;
			searchButtonConstraints.gridy = 0;
			searchButtonConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return searchButtonConstraints;
	}
	
	public void addPartNumberSearchPanelListeners(
			PartNumberSearchPanelListeners partNumberSearchPanelListeners) {
		this.partNumberSearchPanelListeners = partNumberSearchPanelListeners;
	}
}
